package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.systemsetup;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.ConfigKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.SystemsetupKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SystemsetupService extends BaseService {

    private static final Logger logger = LoggerFactory.getLogger(SystemsetupService.class);

    @Resource
    HttpServletRequest httpServletRequest;
    HttpResponse httpResponse;


    /**
     * 登录
     *
     * @param
     * @return
     */
    public RespEntity login(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        String ip = ConfigKit.getStr("systemsetupJava", "config");
        String methodPath = SystemsetupKit.getStr("login", "systemsetup");
        String domain = ip + methodPath;
        String xml = HttpRequest.post(domain)
                .header("Authorization", httpServletRequest.getHeader("Authorization"))
                .form(jsonParam)
                .execute().body();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if (jsonObject.getInteger("code") == 1) {
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if (jsonObject.get("data") != null) {
            return RespEntity.success(jsonObject.get("data"));
        }
        return RespEntity.success();
    }

    /**
     * 刷新token
     *
     * @param
     * @return
     */
    public RespEntity refresh(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        String ip = ConfigKit.getStr("systemsetupJava", "config");
        String methodPath = SystemsetupKit.getStr("refresh", "systemsetup");
        String domain = ip + methodPath;
        String xml = "";
        httpResponse = HttpRequest.post(domain)
                .header("Authorization", httpServletRequest.getHeader("Authorization"))
                .form(jsonParam)
                .execute();
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        logger.info(state + "--------------------------------------");
        if (state == 401 || JSON.parseObject(xml).getInteger("code") == 401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if (jsonObject.getInteger("code") == 1) {
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        logger.info(state + "--------------------------------------");
        if (jsonObject.get("data") != null) {
            com.alibaba.fastjson.JSONObject data = (com.alibaba.fastjson.JSONObject) jsonObject.get("data");
            if (data.get("token") != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("token", data.get("token"));
                return RespEntity.success(map);
            }
        }
        return RespEntity.success();
    }

    /**
     * 登出短token
     *
     * @param
     * @return
     */
    public RespEntity logout(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        String ip = ConfigKit.getStr("systemsetupJava", "config");
        String methodPath = SystemsetupKit.getStr("logout", "systemsetup");
        String domain = ip + methodPath;
        httpResponse = HttpRequest.delete(domain)
                .header("Authorization", httpServletRequest.getHeader("Authorization"))
                .form(jsonParam)
                .execute();
        int state = httpResponse.getStatus();
//		if(state == 200) return RespEntity.success();
//		return RespEntity.fail("登出短token失败");
        return RespEntity.success();
    }

    /**
     * 登出长token
     *
     * @param
     * @return
     */
    public RespEntity logout2(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        logger.info("+++++++++dropoutToken+++++" + jsonParam);
        String ip = ConfigKit.getStr("systemsetupJava", "config");
        String methodPath = SystemsetupKit.getStr("logout2", "systemsetup");
        String domain = ip + methodPath;
        httpResponse = HttpRequest.delete(domain)
                .header("Authorization", httpServletRequest.getHeader("Authorization"))
                .body(jsonParam.toJSONString(1))
                .execute();
        int state = httpResponse.getStatus();
//		if(state == 200) return RespEntity.success();
//		return RespEntity.fail("登出长token失败");
        return RespEntity.success();
    }

    /**
     * 维保到消防重定向
     *
     * @param
     * @return
     */
    public RespEntity redirect(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        String ip = ConfigKit.getStr("systemsetupJava", "config");
        String methodPath = SystemsetupKit.getStr("redirect", "systemsetup");
        String domain = ip + methodPath;
        String xml = "";
        if (httpServletRequest.getMethod().equalsIgnoreCase("GET")) {
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        } else if (httpServletRequest.getMethod().equalsIgnoreCase("POST")) {
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if (jsonObject.getInteger("code") == 1) {
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if (jsonObject.get("data") != null) {
            return RespEntity.success(jsonObject.get("data"));
        }
        return RespEntity.success();
    }

    /**
     * 机构新增
     *
     * @param
     * @return
     */
    public RespEntity orgAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "orgAdd", "systemsetup");
    }

    /**
     * 账户密码修改
     *
     * @param jsonParam
     * @return
     */
    public RespEntity updatePassWord(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "updatePassWord", "systemsetup");
    }

    /**
     * 机构删除
     *
     * @param
     * @return
     */
    public RespEntity orgDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "orgDelete", "systemsetup");
    }

    /**
     * 机构修改
     *
     * @param
     * @return
     */
    public RespEntity orgUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "orgUpdate", "systemsetup");
    }

    /**
     * 机构树形菜单
     *
     * @param
     * @return
     */
    public RespEntity orgTreeList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "orgTreeList", "systemsetup");
    }

    /**
     * 根据父级ID获取机构
     *
     * @param
     * @return
     */
    public RespEntity getPidOrgList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "getPidOrgList", "systemsetup");
    }

    /**
     * 机构详情
     *
     * @param
     * @return
     */
    public RespEntity orgDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "orgDetail", "systemsetup");
    }

    /**
     * 微信授权
     *
     * @param jsonParam
     * @return
     */
    public RespEntity authorize(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return weChatCode(jsonParam, "systemsetupJava", "weChatCode", "systemsetup");
    }

    /**
     * 账号新增
     *
     * @param
     * @return
     */
    public RespEntity userAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "userAdd", "systemsetup");
    }

    /**
     * 账号修改
     *
     * @param
     * @return
     */
    public RespEntity userUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "userUpdate", "systemsetup");
    }

    /**
     * 账号删除
     *
     * @param
     * @return
     */
    public RespEntity userDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "userDelete", "systemsetup");
    }

    /**
     * 账号列表
     *
     * @param
     * @return
     */
    public RespEntity userListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "userListPage", "systemsetup");
    }

    /**
     * 账号详情
     *
     * @param
     * @return
     */
    public RespEntity userDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "userDetail", "systemsetup");
    }

    /**
     * 根据homeId获取user信息
     *
     * @param
     * @return
     */
    public RespEntity getUserList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "getUserList", "systemsetup");
    }

    /**
     * 机构和子机构列表
     *
     * @param
     * @return
     */
    public RespEntity orgAllList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "orgAllList", "systemsetup");
    }

    /**
     * 账号不分页列表
     *
     * @param
     * @return
     */
    public RespEntity userList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "userList", "systemsetup");
    }

    /**
     * 账号权限列表（新增账号的时候用）
     *
     * @param
     * @return
     */
    public RespEntity permissionList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetup(jsonParam, "permissionList", "systemsetup");
    }

    /**
     * 单一机构账户列表
     *
     * @param
     * @return
     */
    public RespEntity userListByOrgId(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "userListByOrgId", "systemsetup");
    }

    /**
     * @param
     * @return
     * @Title: getNewestVersionUpdate <br>
     * @Description: (获取最新版本) <br>
     * @author 石延杰<br>
     */
    public RespEntity getNewestVersionUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "getNewestVersionUpdate", "systemsetup");
    }

    //获取标题图片
    public RespEntity getHeaderTitle(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "getHeaderTitle", "systemsetup");
    }

    /**
     * 设置文字标题
     *
     * @param jsonParam
     * @return
     */
    public RespEntity setTextTitle(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "setTextTitle", "systemsetup");
    }

    /**
     * 获取文字标题
     *
     * @param jsonParam
     * @return
     */
    public RespEntity getTextTitle(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return systemsetupJava(jsonParam, "getTextTitle", "systemsetup");

    }

    /**
     * 获取首页布局信息
     *
     * @param
     * @return
     */
    public RespEntity getHomePageLayoutList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "getHomePageLayoutList", "systemsetup");
    }

    /**
     * 设置首页布局信息
     *
     * @param
     * @return
     */
    public RespEntity setHomePageLayoutList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "setHomePageLayoutList", "systemsetup");
    }

    /**
     * 权限列表
     *
     * @param
     * @return
     */
    public RespEntity permissionsTreeList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "permissionsTreeList", "systemsetup");
    }

    /**
     * 角色新增
     *
     * @param
     * @return
     */
    public RespEntity roleAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "roleAdd", "systemsetup");
    }

    /**
     * 角色删除
     *
     * @param
     * @return
     */
    public RespEntity roleDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "roleDelete", "systemsetup");
    }

    /**
     * 角色详情
     *
     * @param
     * @return
     */
    public RespEntity roleDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "roleDetail", "systemsetup");
    }

    /**
     * 角色编辑
     *
     * @param
     * @return
     */
    public RespEntity roleUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "roleUpdate", "systemsetup");
    }

    /**
     * 角色分页列表
     *
     * @param
     * @return
     */
    public RespEntity roleListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "roleListPage", "systemsetup");
    }

    /**
     * 角色不分页列表
     *
     * @param
     * @return
     */
    public RespEntity roleListNoPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "roleListNoPage", "systemsetup");
    }

    /**
     * 授权
     *
     * @param
     * @return
     */
    public RespEntity authorization(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "authorization", "systemsetup");
    }

    /**
     * 取消授权
     *
     * @param
     * @return
     */
    public RespEntity cancelAuthorization(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "cancelAuthorization", "systemsetup");
    }

    /**
     * 被授权机构列表
     *
     * @param
     * @return
     */
    public RespEntity getOrgAllList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "getOrgAllList", "systemsetup");
    }

    /**
     * 设置处置弹框详情
     *
     * @param
     * @return
     */
    public RespEntity frameMonitoringDetails(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "frameMonitoringDetails", "systemsetup");
    }

    /**
     * 设置处置弹框
     *
     * @param
     * @return
     */
    public RespEntity setFrameMonitoring(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "setFrameMonitoring", "systemsetup");
    }

    /**
     * 大屏设置
     *
     * @param
     * @return
     */
    public RespEntity largeScreenSettings(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "largeScreenSettings", "systemsetup");
    }

    /**
     * 大屏设置列表
     *
     * @param
     * @return
     */
    public RespEntity largeScreenList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "largeScreenList", "systemsetup");
    }

    /**
     * 大屏设置删除
     *
     * @param
     * @return
     */
    public RespEntity largeScreenDel(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "largeScreenDel", "systemsetup");
    }

    /**
     * 创建舆情平台子帐号并绑定
     *
     * @param
     * @return
     */
    public RespEntity accountContactCreateSub(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "accountContactCreateSub", "systemsetup");
    }

    /**
     * 免鉴权平台用户验证并登录
     *
     * @param
     * @return
     */
    public RespEntity accountContactCheck(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "accountContactCheck", "systemsetup");
    }

    /**
     * 注册
     *
     * @param
     * @return
     */
    public RespEntity register(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "register", "systemsetup");
    }

    /**
     * 更新用户绑定单位
     *
     * @param
     * @return
     */
    public RespEntity updateUserHomeId(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "updateUserHomeId", "systemsetup");
    }

    /**
     * 获取验证码（找回密码用）
     *
     * @param
     * @return
     */
    public RespEntity getVerificationCode(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "getVerificationCode", "systemsetup");
    }

    /**
     * 身份验证（找回密码用）
     *
     * @param
     * @return
     */
    public RespEntity identityVerification(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "identityVerification", "systemsetup");
    }

    /**
     * 密码重置（找回密码用）
     *
     * @param
     * @return
     */
    public RespEntity passwordReset(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "passwordReset", "systemsetup");
    }

    /**
     * @Author YangYu
     * @Description 置顶应用新增
     * @Date Created in 14:25 2024/5/11
     * @Param [jsonParam]
     */

    public RespEntity topAppAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "topAppAdd", "systemsetup");
    }

    /**
     * @Author YangYu
     * @Description 置顶应用删除
     * @Date Created in 14:25 2024/5/11
     * @Param [jsonParam]
     */

    public RespEntity topAppDel(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "topAppDel", "systemsetup");
    }

    /**
     * @Author YangYu
     * @Description 置顶应用列表
     * @Date Created in 14:25 2024/5/11
     * @Param [jsonParam]
     */

    public RespEntity topAppList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "topAppList", "systemsetup");
    }

    /**
     * @Author YangYu
     * @Description 更新记录新增、编辑
     * @Date Created in 14:25 2024/5/11
     * @Param [jsonParam]
     */

    public RespEntity renewInfoAddOrUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "renewInfoAddOrUpdate", "systemsetup");
    }

    /**
     * @Author YangYu
     * @Description 更新记录分页列表
     * @Date Created in 14:25 2024/5/11
     * @Param [jsonParam]
     */

    public RespEntity renewInfoListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return systemsetupJava(jsonParam, "renewInfoListPage", "systemsetup");
    }

}
